import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app/bean/article_detail_entity.dart';
import 'package:flutter_app/ui/main/main_logic.dart';
import 'package:flutter_app/utils/DialogUtils.dart';
import 'package:flutter_app/values/MyColors.dart';
import 'package:flutter_app/widget/custom_app_bar.dart';
import 'package:flutter_html/flutter_html.dart';
import 'package:flutter_screenutil/src/size_extension.dart';
import 'package:get/get.dart';

import '../../../r.dart';
import 'news_detail_logic.dart';

/**
 * @Description: 要闻详情
 * @Author: 下雨了
 * @CreateDate: 2022年5月31日 13:59:55
 * @Version: 1.0
 */

///
class NewsDetailPage extends StatefulWidget {
  final String title;

  const NewsDetailPage({
    Key? key,
    required this.title,
  }) : super(key: key);

  @override
  _NewsDetailPageState createState() => _NewsDetailPageState();
}

class _NewsDetailPageState extends State<NewsDetailPage> {
  final logic = Get.put(NewsDetailLogic());
  final state = Get.find<NewsDetailLogic>().state;
  final mainLogic = Get.find<MainLogic>();

  @override
  Widget build(BuildContext context) {
    return GetBuilder<NewsDetailLogic>(builder: (logic) {
      return Scaffold(
        appBar: MyAppBar(context, title: "要闻详情"),
        body: SingleChildScrollView(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Visibility(
                visible: state.progressState,
                child: LinearProgressIndicator(
                  minHeight: 3.r,
                  color: MyColors.FF2AAB33,
                ),
              ),
              Container(
                margin: EdgeInsets.only(left: 16.r, right: 16.r, top: 35.r),
                child: Center(
                  child: Text(
                    state.articleDetailEntity?.title ?? "",
                    textAlign: TextAlign.center,
                    style: TextStyle(color: Colors.black, fontSize: 17.sp, fontWeight: FontWeight.w600),),
                ),
              ),
              Container(
                margin: EdgeInsets.only(left: 16.r, right: 16.r, top: 25.r),
                child: state.articleDetailEntity?.xSource != "" ?
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Flexible(child: Text("来源：" + (state.articleDetailEntity?.xSource ?? ""), maxLines: 1, style: TextStyle(color: MyColors.FF999999, fontSize: 11.sp),)),
                      Text("发布时间：" + (state.articleDetailEntity?.publishTime ?? ""), style: TextStyle(color: MyColors.FF999999, fontSize: 11.sp),),
                    ],
                  )
                  : Text("发布时间：" + (state.articleDetailEntity!.publishTime ?? ""), style: TextStyle(color: MyColors.FF999999, fontSize: 11.sp),),
              ),
              SizedBox(height: 10.r,),
              Divider(indent: 16.r, endIndent: 16.r, color: MyColors.FFDDDDDD, height: 0.r,),
              Container(
                margin: EdgeInsets.only(left: 10.r, right: 10.r),
                child: Html(data: state.articleDetailEntity?.content ?? "",
                  style: {
                    "p": Style(
                      lineHeight: LineHeight(2),
                      fontSize: FontSize(17.sp),
                      fontWeight: FontWeight.w500,
                      direction: TextDirection.ltr,
                      textAlign: TextAlign.justify,
                      letterSpacing: 0.3
                    ),
                    "img": Style(
                      alignment: Alignment.center,
                    ),
                  },
                ),
              ),
              Container(
                alignment: Alignment.centerRight,
                margin: EdgeInsets.only(top: 16.r, right: 16.r, bottom: 20.r),
                child: Text("责任编辑：${state.articleDetailEntity?.updateByName ?? ""}", style: TextStyle(color: MyColors.FF999999, fontSize: 11.sp),),
              ),
              Visibility(
                visible: (state.articleDetailEntity?.fileList?.length ?? 0) > 0,
                child: Container(
                  margin: EdgeInsets.all(16.r),
                  child: Material(
                    color: MyColors.FFF5F5F5,
                    child: Container(
                      padding: EdgeInsets.fromLTRB(8.5.r, 8.5.r, 8.5.r, 8.5.r),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text("附件", style: TextStyle(color: MyColors.FF333333, fontSize: 13.sp, fontWeight: FontWeight.w600),),
                          Container(
                            margin: EdgeInsets.only(top: 6.r),
                            child: ListView.builder(
                              shrinkWrap: true, //解决无线高度问题 解决SingleChildScrollView 和listview冲突问题 ListView.builder
                              padding: EdgeInsets.only(top: 0),
                              itemCount: state.articleDetailEntity?.fileList?.length,
                              physics: NeverScrollableScrollPhysics(),
                              itemBuilder: (BuildContext context, int index) {
                                return _DownLoadListItem(state.articleDetailEntity!.fileList![index]);
                              },
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
      );
    });
  }

  /// 附件下载列表
  Widget _DownLoadListItem(ArticleDetailFileList item) {
    return InkWell(
      child: Container(
        margin: EdgeInsets.only(top: 6.r, bottom: 6.r),
        child: Row(
          children: [
            Container(margin: EdgeInsets.only(top: 2.r, right: 4.5.r),child: Image.asset(R.assetsImgIcon56, width: 14.r,)),
            Text(item.name ?? "", style: TextStyle(color: MyColors.FF2763B9, fontSize: 13.sp),)
          ],
        ),
      ),
      onTap: () {
        dialogUtils.show(
          context: context,
          title: "提示",
          content: "将下载文件到本地",
          callbackOk: () {
            mainLogic.downLoad(item.sourcePath!, item.name!);
          },
        );
      },
    );
  }

  @override
  void dispose() {
    Get.delete<NewsDetailLogic>();
    super.dispose();
  }
}